import java.awt.*;
import javax.swing.*;

import java.awt.event.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

public class SpellingFun extends Game implements KeyListener
{
	private static final int WIDTH = 500;
	private static final int HEIGHT = 400;
	private static String[] words = new String[] {"moja", "mbeely", "tatoo", "nne", "tano", "seeta", "saba", "nane", "tiza", "kumi"};
	private int count = 0;
	private int score = 0;
	private JLabel givenWord, symbol;
	private JButton correctButton, wrongButton;
	private JTextArea right, left;
	private int leftCount, rightCount;
	private Color myRed = new Color(255, 160, 160);
	private Color myGreen = new Color(160, 255, 160);
	//Button handlers:
	private CorrectButton cbHandler;
	private WrongButton wbHandler;
	public HashMap<String,String> dynamicWords = super.wordMap;
	public int currentQuestion;
	public ArrayList<String> questions;
	public ArrayList<String> misspelled;
	public ArrayList<String> wordList;
	private Random r = new Random();
	private int random;
	private ArrayList<Integer> used;

	public SpellingFun(String a, String b)
	{
		super(a,b);
	}

	public void init()
	{

		//words = super.wordMap.values().toArray(new String[super.wordMap.size()]);
		givenWord = new JLabel(words[0], SwingConstants.CENTER);
		givenWord.setSize(30, 400);
		givenWord.setFont(new Font("Calibri", Font.BOLD, 45));
		symbol = new JLabel("Score: " + score, SwingConstants.CENTER);
		left = new JTextArea("");
		left.setEditable(false);
		right = new JTextArea("");
		right.setEditable(false);
		right.setBackground(new Color(255,160,160));
		left.setBackground(new Color(160, 255, 160));
		//SPecify handlers for each button and add (register) ActionListeners to each button.
		correctButton = new JButton("Correct");
		correctButton.setBackground(Color.green);
		cbHandler = new CorrectButton();
		correctButton.addActionListener(cbHandler);
		wrongButton = new JButton("Incorrect");
		wrongButton.setBackground(Color.red);
		wbHandler = new WrongButton();
		wrongButton.addActionListener(wbHandler);

		currentQuestion = 0;
		questions = new ArrayList<String>();
		wordList = new ArrayList<String>();
		for(String str: wordMap.values()){
			questions.add(str);
		}

		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

		// Determine the new location of the window
		int w = this.getSize().width;
		int h = this.getSize().height;
		int x = (dim.width-w)/2;
		int y = (dim.height-h)/2;

		// Move the window
		this.setLocation(x, y); 

		setTitle("Spelling Fun!");
		Container pane = getContentPane();
		pane.setLayout(new GridLayout(3,2));
		pane.addKeyListener(this);
		correctButton.addKeyListener(this);
		wrongButton.addKeyListener(this);
		left.addKeyListener(this);
		right.addKeyListener(this);

		//misspell words and add them to the word list so they can actually be utilized.
		misspelled = misspell();
		for(String str: misspelled)
			wordList.add(str);
		for(String str: questions)
			wordList.add(str);

		//Add things to the pane in the order you want them to appear (left to right, top to bottom)
		pane.add(givenWord);
		pane.add(symbol);
		pane.add(left);
		pane.add(right);
		pane.add(correctButton);
		pane.add(wrongButton);
		leftCount = 0;
		rightCount = 0;
		right.setLineWrap(true);
		left.setLineWrap(true);
		setSize(WIDTH, HEIGHT);
		setVisible(true);
		System.out.println("WordList: "+ wordList);
		random = r.nextInt(wordList.size());
		used = new ArrayList<Integer>();
		used.add(random);
		givenWord.setText(wordList.get(random));
	}

	private class CorrectButton implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			if(count <= wordList.size())
			correctPressed();

		}
	}

	public class WrongButton implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			if(count <= wordList.size())
			wrongPressed();
		}
	}
	@Override
	public void keyPressed(KeyEvent event) {

	}

	@Override
	public void keyReleased(KeyEvent event) {
		// TODO Auto-generated method stub

		int key = event.getKeyCode();
		if (key == KeyEvent.VK_LEFT) {
			if(count<= wordList.size())
			correctPressed();

		}
		else if (key == KeyEvent.VK_RIGHT) {
			if(count<=wordList.size())
			wrongPressed();
		}
	}


	@Override
	public void keyTyped(KeyEvent event) {


	}

	/**
	 * obtain random misspelled words from the questions ArrayList
	 * about half of the words provided will be misspelled
	 * @return an assortion of misspelled words
	 */
	public ArrayList<String> misspell()
	{
		ArrayList<String> misspelled = new ArrayList<String>();
		ArrayList<Integer> picked = new ArrayList<Integer>();

		r = new Random();
		int misspellings = questions.size()/2;

		for (int i = 0; i < misspellings;i++){
			int j = r.nextInt(questions.size());
			while(picked.contains(j))
				j = r.nextInt(questions.size());
			picked.add(j);
			misspelled.add(questions.remove(j));
			System.out.println("incorrectly spelled: " + misspelled);
			System.out.println("correctly spelled: " + questions);
		}

		for(int i = 0; i < misspelled.size();i++){
			String word = misspelled.get(i);
			String newWord = "";
			int j = r.nextInt(word.length());
			char copy = word.charAt(j);

			//j is position that is going to be duplicated
			for(int k = 0;k<j;k++)
				newWord += word.charAt(k);
			//add the char that was originally at position j twice
			newWord+=word.charAt(j);
			newWord+=word.charAt(j);
			System.out.println(newWord);
			//add every other character that was originally in the word, check if j is at max length of
			//original word first
			if(j!= word.length()-1)
				for (int k = j+1;k<word.length();k++)
					newWord+=word.charAt(k);
			System.out.println(newWord);

			misspelled.set(i, newWord);
		}

		System.out.println("final misspellings: "+ misspelled);
		return misspelled;
	}

	public boolean checkCorrect(String word){
		return (questions.contains(word));
	}

	public boolean checkIncorrect(String word){
		return(misspelled.contains(word));
	}

	public void wrongPressed(){
		count ++;
		
			if(misspelled.contains(wordList.get(random))){
				score++;
				this.setScore( (int)(((double) score/wordList.size())*100) );
				symbol.setText("Score: " + score);
				right.append(""+wordList.get(random));
				right.append("  ");
			}
			else{

				left.append("*"+wordList.get(random));
				left.append("*  ");

			}

		if(count < wordList.size()){

			random = r.nextInt(wordList.size());
			while(used.contains(random)){
				random = r.nextInt(wordList.size());
			}
			used.add(random);

			givenWord.setText(wordList.get(random));
		}

		else{

			if(misspelled.contains(wordList.get(random))){
				score++;
				this.setScore( (int)(((double) score/wordList.size())*100) );
				symbol.setText("Score: " + score);
				right.append(""+wordList.get(random));
				right.append("  ");
			}
			else{

				left.append("*"+wordList.get(random));
				left.append("*  ");

			}

			end();

		}

	}

	public void correctPressed(){
		count ++;



		if(questions.contains(wordList.get(random))){
			score++;
			this.setScore( (int)(((double) score/wordList.size())*100) );
			System.out.println((int)(((double) score/wordList.size())*100) );
			symbol.setText("Score: " + score);

			left.append(wordList.get(random));
			left.append("  ");
		}
			else{

				right.append("*"+wordList.get(random));
				right.append("*  ");

			}
			
		if(count < wordList.size()){
			random = r.nextInt(wordList.size());

			while(used.contains(random)){
				random = r.nextInt(wordList.size());
			}

			used.add(random);
			givenWord.setText(wordList.get(random));
		}
		else{

			end();

		}
	}

	public void end(){
		givenWord.setText("Finished!");
		wrongButton.removeActionListener(wbHandler);
		correctButton.removeActionListener(cbHandler);
		
	}

}
